package com.yanq.liancar.base;
/*
 *  @创建者     liyan
 *  @创建时间   2018/11/8  10:38
 *  @描述       ${TODO}
 */


import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gyf.barlibrary.ImmersionBar;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class LyBaseFrament extends Fragment {
    protected Activity mActivity;

    protected View mRootView;


    /**
     * 是否对用户可见
     */

    protected boolean mIsVisible;

    /**
     * 是否加载完成
     * <p>
     * 当执行完onViewCreated方法后即为true
     */

    protected boolean mIsPrepare;


    /**
     * 是否加载完成
     * <p>
     * 当执行完onViewCreated方法后即为true
     */

    protected boolean mIsImmersion;


    protected ImmersionBar mImmersionBar;

    private Unbinder unbinder;


    @Override

    public void onAttach(Context context) {

        super.onAttach(context);

        mActivity = (Activity) context;

    }


    @Nullable

    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mRootView = inflater.inflate(setLayoutId(), container, false);

        return mRootView;

    }


    @Override

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);

        unbinder = ButterKnife.bind(this, mRootView);

        if (isLazyLoad()) {

            mIsPrepare = true;

            mIsImmersion = true;

            onLazyLoad();

        } else {

            initData();

            if (isImmersionBarEnabled())

                initImmersionBar();

        }

        initView();

        setListener();

    }


    @Override

    public void onDestroy() {

        super.onDestroy();

        unbinder.unbind();

        if (mImmersionBar != null)

            mImmersionBar.destroy();

    }


    @Override

    public void setUserVisibleHint(boolean isVisibleToUser) {

        super.setUserVisibleHint(isVisibleToUser);


        if (getUserVisibleHint()) {

            mIsVisible = true;

            onVisible();

        } else {

            mIsVisible = false;

            onInvisible();

        }

    }


    /**
     * 是否懒加载
     *
     * @return the boolean
     */

    protected boolean isLazyLoad() {

        return true;

    }


    /**
     * 是否在Fragment使用沉浸式
     *
     * @return the boolean
     */

    protected boolean isImmersionBarEnabled() {

        return true;

    }


    /**
     * 用户可见时执行的操作
     */

    protected void onVisible() {

        onLazyLoad();

    }


    private void onLazyLoad() {

        if (mIsVisible && mIsPrepare) {

            mIsPrepare = false;

            initData();

        }

        if (mIsVisible && mIsImmersion && isImmersionBarEnabled()) {

            initImmersionBar();

        }

    }


    /**
     * Sets layout id.
     *
     * @return the layout id
     */

    protected abstract int setLayoutId();


    /**
     * 初始化数据
     */

    protected void initData() {


    }


    /**
     * 初始化沉浸式
     */

    protected void initImmersionBar() {

        mImmersionBar = ImmersionBar.with(getActivity());

        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();

    }


    /**
     * view与数据绑定
     */

    protected void initView() {


    }


    /**
     * 设置监听
     */

    protected void setListener() {


    }


    /**
     * 用户不可见执行
     */

    protected void onInvisible() {


    }


    /**
     * 找到activity的控件
     *
     * @param <T> the type parameter
     * @param id  the id
     * @return the t
     */

    @SuppressWarnings("unchecked")

    protected <T extends View> T findActivityViewById(@IdRes int id) {

        return (T) mActivity.findViewById(id);

    }
}
